/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#include "ge_local.h"


/*-----------------------------------------------------------------------------
	Export stuff :
-----------------------------------------------------------------------------*/

EGeometryFactory	static_ge;

IGeometryFactory *Geometry( void )
{
	return &static_ge;
}

/*-----------------------------------------------------------------------------
	Main stuff :
-----------------------------------------------------------------------------*/

EGeometryFactory::EGeometryFactory( void )	{	}
EGeometryFactory::~EGeometryFactory( void )	{	}

ITriMesh		*EGeometryFactory::CreateTriMesh( uint format )	{	return new ETriMesh(format);	}
IScene			*EGeometryFactory::CreateScene( void )			{	return new EScene(this);		}
ISceneNode		*EGeometryFactory::CreateSceneNode( void )		{	return new ESceneNode(this);	}
IAnimTrack		*EGeometryFactory::CreateAnimTrack( void )		{	return new EAnimTrack();		}
/*-----------------------------------------------------------------------------
	Scene cache :
-----------------------------------------------------------------------------*/

//
//	EGeometryFactory::ClearSceneCache
//
void EGeometryFactory::ClearSceneCache( void )
{
	scenes.clear();
	anim_tracks.clear();
}


//
//	EGeometryFactory::LoadSceneFromFile
//
IPxScene EGeometryFactory::LoadSceneFromFile( const EString file_scene_node_path )
{
	uint	stick_pos	=	file_scene_node_path.Find('|');
	EString	filepath	=	file_scene_node_path.Substr( 0, stick_pos );	

	//
	//	search for previously loaded scene :
	//
	for (uint i=0; i<scenes.size(); i++) {
		if (scenes[i].name == filepath) {
			return scenes[i].scene;
		}
	}

	//
	//	start loading :
	//	
	LOGF("Loading : %s", filepath.CStr());

	//if( !strcmp( filepath.CStr() + strlen(filepath.CStr()) - 4, ".fbx") )
	//{
	//	return LoadFBXScene(filepath);
	//}

	//
	//	open file :
	//
	IHrFile	f = fs()->openFile( filepath.CStr(), FS_OPEN_READ );
	
	std::vector<char>	buffer;
	buffer.resize(f->size()+1, '\0');
	
	f->read(&buffer[0], f->size());
	f = NULL;
	
	//
	//	create scene and parse XML :
	//
	IPxScene	scene	=	ge()->CreateScene();
	
	xml_document<>	xdoc;
	xdoc.parse<0>(&buffer[0]);

	xml_node<> *xscene	=	xdoc.first_node("scene");

	if (!xscene) { 
		RUNTIME_ERROR("<scene> section not found"); 
	}

	scene->ReadXML( xscene );
	
	//
	//	add to scene list :
	//
	scene_s sc;
	sc.name		=	filepath;
	sc.scene	=	scene;
	scenes.push_back( sc );
	
	return scene;	
}


//
//	EGeometryFactory::LoadSceneNodeFromFile
//
IPxSceneNode EGeometryFactory::LoadSceneNodeFromFile( const EString file_scene_node_path )
{
	uint n = file_scene_node_path.Find('|');

	if (n==EString::NPOS) {
		RUNTIME_ERROR(va("path '%s' must be <file path|hierarchy path>", file_scene_node_path.CStr()));
	}

	IPxScene	scene	=	LoadSceneFromFile( file_scene_node_path );
	
	IPxSceneNode snode	=	scene->GetNodeByPath( file_scene_node_path.CStr() );
	
	if (!snode) {
		RUNTIME_ERROR(va("scene node '%s' not found", file_scene_node_path.CStr()));
	}

	return snode;	
}


//
//	EGeometryFactory::LoadMeshFromFile
//
IPxTriMesh EGeometryFactory::LoadMeshFromFile( const EString file_scene_node_path )
{
	IPxSceneNode	snode = LoadSceneNodeFromFile( file_scene_node_path );

	IPxTriMesh	mesh	=	snode->GetMesh();
	
	if (!mesh) {
		RUNTIME_ERROR(va("scene node '%s' does not have mesh", file_scene_node_path.CStr()));
	}

	return mesh;
}


//
//	EGeometryFactory::LoadAnimTrack
//
IPxAnimTrack EGeometryFactory::LoadAnimTrack( const EString path )
{
	for (uint i=0; i<anim_tracks.size(); i++) {
		if (anim_tracks[i].name	== path) {
			return anim_tracks[i].anim_track;
		}
	}

	IHrFile	f = fs()->openFile(path.CStr(), FS_OPEN_READ );
	
	IPxBlob	buffer	=	f->stringBlob();
	//std::vector<char>	buffer;
	//buffer.resize(f->size()+1, '\0');
	//
	//f->read(&buffer[0], f->size());
	//f = NULL;
	
	//
	//	create scene and parse XML :
	//
	IPxAnimTrack	anim_track	=	ge()->CreateAnimTrack();
	
	xml_document<>	xdoc;
	xdoc.parse<0>((char*)buffer->GetBufferPointer());

	xml_node<> *xanimation	=	xdoc.first_node("animation");
	
	if (!xanimation) { 
		RUNTIME_ERROR("<animation> section not found"); 
	}

	anim_track->ReadXML( xanimation );
	
	anim_track_s	at;
	at.anim_track	=	anim_track;
	at.name			=	path;
	
	anim_tracks.push_back( at );
	
	return anim_track;
}

